package com.haoshuang.zhouzhoubang.ui.page.study.train.child

import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.FragmentTrainExamArticleBinding
import com.haoshuang.zhouzhoubang.model.bean.TrainArticleBean
import com.haoshuang.zhouzhoubang.model.bean.TrainLabelBean
import com.haoshuang.zhouzhoubang.ui.page.study.child.StudyArticleListAdapter
import com.haoshuang.zhouzhoubang.ui.page.study.detail.TrainDetailsActivity
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.horizontal
import com.mufeng.libs.utils.view.vertical
import com.qw.curtain.lib.Curtain
import com.qw.curtain.lib.shape.RoundShape

/**
 *
 */
class TrainExamArticleFragment :
    BaseVMFragment<TrainExamArticleViewModel, FragmentTrainExamArticleBinding>() {

    companion object {
        fun getInstance(): TrainExamArticleFragment {
            val fragment = TrainExamArticleFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    // 顶部标签
    private val labelList = mutableListOf<TrainLabelBean>()
    private val labelAdapter = TrainLabelAdapter(labelList)

    // 文章列表
    private val data = arrayListOf<TrainArticleBean>()
    private val adapter = StudyArticleListAdapter(data)

    private var type_id = ""

    override fun initView(savedInstanceState: Bundle?) {

        binding.recyclerView.vertical().adapter = adapter
        adapter.setEmptyData()

        labelAdapter.setOnItemClick { view, position, item ->
            if (item?.isCheck == 0) {
                labelList.forEach {
                    if (it.id == item.id) {
                        it.isCheck = 1
                    } else {
                        it.isCheck = 0
                    }
                }
                type_id = item.id.toString()
                isShowing = true
                p = 1
                vm.getTrainExamArticleList(type_id, p)
            }
            labelAdapter.notifyDataSetChanged()
        }

        adapter.setOnItemClick { view, position, item ->
            TrainDetailsActivity.launch(
                mActivity,
                1,
                item?.id,
                "${URLConstants.baseUrl}${item?.detail_url}",
                item?.type_id != 9 || item.is_order_eligibility != 1
            )
        }

        binding.rvTrainingLabel.horizontal().adapter = labelAdapter
        binding.refreshLayout.setOnRefreshListener {
            isShowing = false
            p = 1
            vm.getTrainExamArticleList(type_id, p)
        }

        binding.refreshLayout.setOnLoadMoreListener {
            isShowing = false
            p++
            vm.getTrainExamArticleList(type_id, p)
        }

    }
    private fun showGuide(){
        binding.recyclerView.post {
            val item = binding.recyclerView.getChildAt(0)
            Curtain(mActivity)
                .with(item)
                .withShape(item, RoundShape(12f))
                .setTopView(R.layout.guide_study2)
                .addOnTopViewClickListener(R.id.tvNext){ current, host ->
                    AccountUtils.studyStep2Guide = true
                    host.dismissGuide()
                }
                .show()
        }


    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        if (type_id.isNotEmpty()) {
            isShowing = true
            p = 1
            vm.getTrainExamArticleList(type_id, p)
        }
    }

    override fun lazyLoadData() {
        vm.getTrainLabel()
    }

    override fun startObserve() {

        vm.getTrainLabelData.observeState(this) {
            onLoading { }
            onError { code, msg -> }
            onSuccess {
                labelList.clear()
                labelList.addAll(it?.data ?: emptyList())
                if (labelList.isNotEmpty()) {
                    labelList.first().isCheck = 1
                    type_id = labelList.first().id.toString()
                    isShowing = true
                    p = 1
                    vm.getTrainExamArticleList(type_id, p)
                }
                labelAdapter.notifyDataSetChanged()
            }
        }

        vm.getTrainExamArticleListData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                adapter.type_id = type_id
                binding.refreshLayout.setupData(it?.data, data, adapter, p)
                if (!AccountUtils.studyStep2Guide) {
                    showGuide()
                }
            }
        }

    }

}